<?php

/**
 * 上传控制器
 */
class UeditorController extends \Our\Controller_AbstractAdmin
{
    /**
     * ueditor调用的接口
     * @return void|null
     */
    public function indexAction()
    {
        $action = empty($_GET['action']) ? NULL : $_GET['action'];
        if ($action === "config") {
            return $this->configAction();
        } else if ($action === "uploadfile") {
            return $this->uploadfileAction();
        } else if ($action === "uploadimage") {
            return $this->uploadimageAction();
        }
        return null;
    }

    /**
     * ueditor获取配置
     */
    public function configAction()
    {
        $this->_display('ueditor/config.phtml');
    }

    /**
     * ueditor上传图片功能
     */
    public function uploadimageAction()
    {
        $result = $this->_upload();
        if ($result['status'] === 1) {
            echo json_encode([
                "state" => "SUCCESS",
                "url" => $result['dest_file_name'],
            ]);
        } else {
            echo json_encode([
                "state" => "ERROR"
            ]);
        }
    }

    /**
     * ueditor上传文件功能
     */
    public function uploadfileAction()
    {
        $imageSize = 20000;
        //上传到本地服务器
        $uploadConfig = new \Our\Upload\Config(array(
                'dir' => '/upload/tmp',
                'fileName' => 'upload_image_file',
                'saveFilename' => md5(time() . rand(10000, 99999)),
                'size' => $imageSize * 1024)
        );
        $upload = new \Our\Upload\Main($uploadConfig);
        $result = $upload->upload();
        echo json_encode([
            "state" => "SUCCESS",
            "url" => "/upload/tmp/814e3bf3342a81e3cc6a7329bffadc20.jpg",
        ]);
    }


}
